package com.hnevc.hitmouse;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    public boolean isPlaying=false;
    public static final int WHAT = 0x101;
    private  int i=0;//记录打到的地鼠的数量
    private ImageView mouse;//地鼠图片
    private ImageView bg;
    private ImageView hummer;
    private ImageView honorbg;
    private ImageView level;//等级图片
    private TextView mTvCount;//达到的地鼠数量显示文本框
    private Handler mHandler;//申明一个handler
    private int totalMouse;//总地鼠数
    public int[][] position =new int[][]{
            {350,600},{700,650},{850,750},{890,530},{870,450},{1200,430},{1400,530},{1300,660}
    };//地洞的位置
    private MediaPlayer mMediaPlayer;
    private MediaPlayer mMediaPlayer2;
    private MediaPlayer mMediaPlayer3;
    private MediaPlayer mMediaPlayer4;
    private ImageButton mBtnStart;
    private Thread mPlayGameThread;
    private ImageView mBtnExit;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPlayGameThread = new Thread(new Runnable() {
            @Override
            public void run() {

                int index =0;
                while((totalMouse--)>0){
                    index = new Random().nextInt(position.length);
                    Message m = mHandler.obtainMessage();
                    m.arg1 = index;
                    m.what = WHAT;
                    mHandler.sendMessage(m);
                    try {
                        Thread.sleep(new Random().nextInt(500)+500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(mMediaPlayer.isPlaying()) {
                            mMediaPlayer.pause();
                        }
                        if(!mMediaPlayer4.isPlaying()) {
                            mMediaPlayer4.start();
                        }
                        honorbg.setVisibility(View.VISIBLE);
                        if(i >=15){
                            level.setImageResource(R.drawable.level1);
                        }else if(i>=13){
                            level.setImageResource(R.drawable.level2);
                        }else {
                            level.setImageResource(R.drawable.level3);
                        }
                        level.setVisibility(View.VISIBLE);
                        hummer.setVisibility(View.INVISIBLE);
                        mBtnStart.setVisibility(View.VISIBLE);
                        mTvCount.setText(i+"");
                        mTvCount.setVisibility(View.VISIBLE);
                        isPlaying=false;
                    }
                });



            }
        });
        initView();

        initEvent();




        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                int index =0;
                if(msg.what == WHAT){
                    index = msg.arg1;
                    mouse.setX(position[index][0]);
                    mouse.setY(position[index][1]);
                    mouse.setVisibility(View.VISIBLE);
                }
            }
        };



    }

    private void initGame() {
        i=0;//初始化游戏数据
        totalMouse=20;
        honorbg.setVisibility(View.INVISIBLE);
        level.setVisibility(View.INVISIBLE);
        mBtnStart.setVisibility(View.INVISIBLE);
        mTvCount.setVisibility(View.INVISIBLE);
        if(mMediaPlayer4!=null) {
            mMediaPlayer4.pause();
        }else{
            mMediaPlayer4 = MediaPlayer.create(this, R.raw.honor);
            mMediaPlayer4.setLooping(true);

        }
        if(mMediaPlayer==null) {
            //播放背景音乐
            mMediaPlayer = MediaPlayer.create(this, R.raw.bg4);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.start();
        }else{

            mMediaPlayer.start();
        }
        if(mMediaPlayer2==null) {
            mMediaPlayer2 = MediaPlayer.create(this, R.raw.boom);
        }
        if(mMediaPlayer3==null) {
            mMediaPlayer3 = MediaPlayer.create(this, R.raw.a);
        }


    }

    private void initEvent() {
        bg.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(isPlaying) {
                    hummer.setX(event.getX());
                    hummer.setY(event.getY());
                    hummer.setVisibility(View.VISIBLE);
                    mMediaPlayer2.start();
                    return true;
                }
                return false;
            }
        });
        mouse.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(isPlaying) {
                    v.setVisibility(View.INVISIBLE);
                    i++;
                    mMediaPlayer3.start();
                    return false;
                }
                return true;
            }
        });
        mBtnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isPlaying==false) {
                    isPlaying = true;
                    mBtnStart.setVisibility(View.INVISIBLE);
                    initGame();
                    mPlayGameThread.start();
                }
            }
        });
        mBtnExit.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                MainActivity.this.finish();
                return false;
            }
        });
    }

    private void initView() {
        mouse = (ImageView) findViewById(R.id.id_iv_mouse);
        bg = (ImageView) findViewById(R.id.id_iv_bg);
        hummer = (ImageView) findViewById(R.id.id_iv_hummer);
        honorbg = (ImageView) findViewById(R.id.id_iv_honorbg);
        level = (ImageView) findViewById(R.id.id_iv_level);
        mBtnStart = (ImageButton)findViewById(R.id.id_btn_start);
        mTvCount = (TextView) findViewById(R.id.id_tv_i);
        mBtnExit = (ImageView)findViewById(R.id.id_iv_exit);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mMediaPlayer.isPlaying()){
            mMediaPlayer.stop();
            mMediaPlayer.release();
        }
        mMediaPlayer2.release();
        mMediaPlayer3.release();
        if(mMediaPlayer4.isPlaying())
        {
            mMediaPlayer4.stop();
            mMediaPlayer4.release();
        }
    }
}
